package org.jypj.dev.service.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.jypj.dev.model.TrainWork;
import org.jypj.dev.dao.ITrainWorkDao;
import org.jypj.dev.service.ITrainWorkService;
import org.jypj.dev.util.Page;

@Service("trainWorkService")
public class TrainWorkServiceImpl implements ITrainWorkService {

    @Resource
    private ITrainWorkDao trainWorkDao;

    /**
     * 保存
     * 字段为空的不存防止覆盖存在默认值的字段
     *
     * @param trainWork
     * @return 保存后的对象包括ID
     */
    public int saveTrainWorkByField(TrainWork trainWork) {
        String uuid = UUID.randomUUID().toString().replace("-", "").trim();
        trainWork.setId(uuid);
        return trainWorkDao.saveTrainWorkByField(trainWork);
    }

    /**
     * 保存
     * 所有字段全都保存
     *
     * @param trainWork
     * @return 保存后的对象包括ID
     */
    public int saveTrainWork(TrainWork trainWork) {
        String uuid = UUID.randomUUID().toString().replace("-", "").trim();
        trainWork.setId(uuid);
        return trainWorkDao.saveTrainWork(trainWork);
    }

    /**
     * 根据ID删除
     *
     * @param id 主键ID
     * @return 删除记录数
     */
    public int deleteTrainWorkById(String id) {

        return trainWorkDao.deleteTrainWorkById(id);
    }

    /**
     * 根据对象删除
     *
     * @param id 主键ID
     * @return
     */
    public int deleteTrainWorkByObject(TrainWork trainWork) {

        return trainWorkDao.deleteTrainWorkByObject(trainWork);
    }

    /**
     * 更新
     * 只更新值不为空的字段
     *
     * @param trainWork
     * @return 保存后的对象包括ID
     */
    public int updateTrainWorkByField(TrainWork trainWork) {

        return trainWorkDao.updateTrainWorkByField(trainWork);
    }

    /**
     * 更新
     * 更新所有字段
     *
     * @param trainWork
     * @return 保存后的对象包括ID
     */
    public int updateTrainWork(TrainWork trainWork) {

        return trainWorkDao.updateTrainWork(trainWork);
    }

    /**
     * 更新
     * 更新所有字段
     *
     * @return 保存后的对象包括ID
     */
    public int updateTrainWorkValid(String dateStr) {
        trainWorkDao.updateTrainWorkValidByDate2(dateStr);
        trainWorkDao.updateTrainWorkValidByDate(dateStr);
        return 0;
    }

    /**
     * 按ID查询
     *
     * @return TrainWork
     * @parm id 主键ID
     */
    public TrainWork selectTrainWorkById(String id) {

        return trainWorkDao.selectTrainWorkById(id);
    }

    /**
     * 分页查询 包含条件
     *
     * @param page 分页对象
     * @param map  查询条件
     * @return List<TrainWork>
     */
    public Page selectOnePageByMap(Page page, Map<String, Object> map) {
        List<TrainWork> trainWorks = trainWorkDao.selectOnePageByMap(page, map);
        if (trainWorks != null && trainWorks.size() > 0) {
            page.setResult(trainWorks);
        } else {
            page.setResult(new ArrayList<TrainWork>());
        }
        return page;
    }

    /**
     * 分页查询 包含对象条件
     *
     * @param page      分页对象
     * @param trainWork 查询条件
     * @return Page
     */
    public Page selectOnePageByTrainWork(Page page, TrainWork trainWork) {
        List<TrainWork> trainWorks = trainWorkDao.selectOnePageByTrainWork(page, trainWork);
        if (trainWorks != null && trainWorks.size() > 0) {
            page.setResult(trainWorks);
        } else {
            page.setResult(new ArrayList<TrainWork>());
        }
        return page;
    }

    /**
     * 按条件查询全部的
     *
     * @param map 查询条件
     * @return List<TrainWork>
     */
    public List<TrainWork> selectAllByMap(Map<String, Object> map) {
        return trainWorkDao.selectAllByMap(map);
    }


    /**
     * 按条件查询全部的
     *
     * @param map 查询条件
     * @return List<TrainWork>
     */
    public List<TrainWork> selectAllByTrainWork(TrainWork trainWork) {

        return trainWorkDao.selectAllByTrainWork(trainWork);
    }

    /**
     * 按条件查询单个对象
     *
     * @param map 查询条件
     * @return TrainWork
     */
    public TrainWork selectObjectByMap(Map<String, Object> map) {

        return trainWorkDao.selectObjectByMap(map);
    }

    /**
     * 按条件查询单个对象
     *
     * @param map 查询条件
     * @return TrainWork
     */
    public TrainWork selectObjectByTrainWork(TrainWork trainWork) {

        return trainWorkDao.selectObjectByTrainWork(trainWork);
    }
}